﻿using System;
using System.IO;
using System.Reflection;
using MyFramework.Core;

namespace MyFramework.Shell.ViewModels
{
    /// <summary>
    /// Email notification view model
    /// </summary>
    public abstract class EmailNotificationViewModel : ViewModelBase
    {
        #region Static Methods

        /// <summary>
        /// Reads the template.
        /// </summary>
        /// <returns></returns>
        protected static string ReadTemplate(string template)
        {
            string retVal = null;
            try
            {
                StreamReader sr = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream(template));
                retVal = sr.ReadToEnd();
                sr.Close();
            }
            catch (Exception ex)
            {
                throw new Exception("Error Reading File. " + ex.Message);
            }

            return retVal;
        }

        /// <summary>
        /// Patches the template.
        /// </summary>
        /// <param name="template">The template.</param>
        /// <returns></returns>
        protected static string PatchTemplate(string template)
        {
            int startIndex = template.IndexOf("<style");
            int endIndex = template.IndexOf("</style>");
            if (startIndex == -1 || endIndex == -1)
            {
                return template;
            }

            string stylePart = template.Substring(startIndex, endIndex - startIndex);
            stylePart = stylePart.Replace("{", "{{");
            stylePart = stylePart.Replace("}", "}}");
            template = template.Substring(0, startIndex) + stylePart + template.Substring(endIndex, template.Length - endIndex);
            return template;
        }

        /// <summary>
        /// Formats the HTML.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <returns></returns>
        protected static string FormatHtml(string message)
        {
            return message.Replace("\n", "<br/>");
        }

        #endregion

    }
}
